package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IAttdSyncRecordBO;
import com.wyzz.hr.dao.IAttdSyncRecordDAO;
import com.wyzz.hr.pojo.AttdMachine;
import com.wyzz.hr.pojo.AttdSyncRecord;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class AttdSyncRecordBOImpl implements IAttdSyncRecordBO {

	public AttdSyncRecordBOImpl() {
	}

	public IAttdSyncRecordDAO getAttdSyncRecordDAO() {
		return attdSyncRecordDAO;
	}

	public void setAttdSyncRecordDAO(IAttdSyncRecordDAO attdSyncRecordDAO) {
		this.attdSyncRecordDAO = attdSyncRecordDAO;
	}

	public boolean addAttdSyncRecord(AttdSyncRecord data) {
		try {
			attdSyncRecordDAO.saveObject(data);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	public boolean deleteAttdSyncRecord(String srcId) {
		AttdSyncRecord data;
		try {
			data = (AttdSyncRecord) attdSyncRecordDAO.loadObject(
					AttdSyncRecord.class, srcId, null, new boolean[0]);
			if (data == null)
				return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		attdSyncRecordDAO.deleteObject(data);
		return true;
	}

	public boolean deleteAttdSyncRecord(List recordList) {
		try {
			attdSyncRecordDAO.deleteCollection(recordList);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public AttdSyncRecord getAttdSyncRecord(String srcId) {
		return (AttdSyncRecord) attdSyncRecordDAO.loadObject(
				AttdSyncRecord.class, srcId, null, new boolean[0]);
	}

	public List getAttdSyncRecord(String recordIds[]) {
		DetachedCriteria dc = DetachedCriteria.forClass(AttdSyncRecord.class);
		dc.createAlias(AttdSyncRecord.PROP_ASR_EMP_NO,
				AttdSyncRecord.PROP_ASR_EMP_NO, 1);
		dc.createAlias(AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO,
				AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO, 1);
		dc.add(Restrictions.in(AttdSyncRecord.PROP_ASR_ID, recordIds));
		List result = attdSyncRecordDAO.findByCriteria(dc);
		if (result.isEmpty())
			return null;
		else
			return result;
	}

	public List getAttdSyncRecordList(DetachedCriteria dc, Pager page) {
		if (page != null) {
			BaseAction.addOrders(dc, page, new String[] { (new StringBuilder())
					.append("emp.").append(Employee.PROP_EMP_DISTINCT_NO)
					.append("-up").toString() });
			page.splitPage(dc);
			return attdSyncRecordDAO.findByCriteria(dc, page.getPageSize(),
					page.getCurrentPage());
		} else {
			BaseAction.addOrders(dc, null, new String[] { (new StringBuilder())
					.append("emp.").append(Employee.PROP_EMP_DISTINCT_NO)
					.append("-up").toString() });
			return attdSyncRecordDAO.findByCriteria(dc);
		}
	}

	public boolean insertDataList(List dataList) {
		attdSyncRecordDAO.saveOrupdate(dataList);
		return true;
	}

	public boolean saveAttdSyncRecord(AttdSyncRecord data) {
		attdSyncRecordDAO.saveOrupdate(data);
		return true;
	}

	private String[] getEmpIds(List dataList) {
		String empIds[] = new String[dataList.size()];
		int count = 0;
		for (Iterator i$ = dataList.iterator(); i$.hasNext();) {
			AttdSyncRecord data = (AttdSyncRecord) i$.next();
			empIds[count] = data.getAsrEmp().getId();
			count++;
		}

		return empIds;
	}

	public boolean batchAttdSyncRecord(List attdSyncRecordList) {
		attdSyncRecordDAO.saveOrupdate(attdSyncRecordList);
		return true;
	}

	public boolean deleteAttdSyncRecord(List employeeList, AttdMachine machine) {
		Integer empMachineNos[] = new Integer[employeeList.size()];
		Map map = new HashMap();
		for (int i = 0; i < employeeList.size(); i++) {
			empMachineNos[i] = ((Employee) employeeList.get(i))
					.getEmpMachineNo();
			map.put(((Employee) employeeList.get(i)).getEmpMachineNo(),
					employeeList.get(i));
		}

		List result = getAttdSyncRecord(empMachineNos, machine.getMacId());
		if (result == null)
			return false;
		List deleteList = new ArrayList();
		List updateList = new ArrayList();
		for (Iterator i$ = result.iterator(); i$.hasNext();) {
			AttdSyncRecord record = (AttdSyncRecord) i$.next();
			Employee emp = (Employee) map.get(record.getAsrEmpMachineNo());
			if (emp.getSyncResult().intValue() == 1) {
				deleteList.add(record);
			} else {
				record.setAsrSync(Integer.valueOf(0));
				updateList.add(record);
			}
		}

		attdSyncRecordDAO.deleteCollection(deleteList);
		attdSyncRecordDAO.saveOrupdate(updateList);
		return true;
	}

	public List getAttdSyncRecord(Integer empMachineNos[], String machineId) {
		DetachedCriteria dc = DetachedCriteria.forClass(AttdSyncRecord.class);
		dc.createAlias(AttdSyncRecord.PROP_ASR_EMP_NO,
				AttdSyncRecord.PROP_ASR_EMP_NO, 1);
		dc.createAlias(AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO,
				AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO, 1);
		if (empMachineNos != null && empMachineNos.length > 0)
			dc.add(Restrictions.in(AttdSyncRecord.PROP_ASR_EMP_MACHINE_NO,
					empMachineNos));
		BaseCrit.addDC(dc, AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO,
				AttdMachine.PROP_MAC_ID, new Object[] { new AttdMachine(
						machineId) });
		List recordList = attdSyncRecordDAO.findByCriteria(dc);
		if (recordList.isEmpty())
			return null;
		else
			return recordList;
	}

	public List getAllRecordHasEmp() {
		DetachedCriteria dc = DetachedCriteria.forClass(AttdSyncRecord.class);
		dc.createAlias(AttdSyncRecord.PROP_ASR_EMP_NO,
				AttdSyncRecord.PROP_ASR_EMP_NO, 1);
		dc.createAlias(AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO,
				AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO, 1);
		dc.add(Restrictions.isNotNull(AttdSyncRecord.PROP_ASR_EMP_NO));
		List recordList = attdSyncRecordDAO.findByCriteria(dc);
		if (recordList.isEmpty())
			return null;
		else
			return recordList;
	}

	public List getRecListFromMachine(String machineIdArr[]) {
		DetachedCriteria dc = DetachedCriteria.forClass(AttdSyncRecord.class);
		dc.createAlias(AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO,
				AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO, 1);
		dc.add(Restrictions.in((new StringBuilder()).append(
				AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO).append(".").append(
				AttdMachine.PROP_MAC_ID).toString(), machineIdArr));
		List recordList = attdSyncRecordDAO.findByCriteria(dc);
		return recordList;
	}

	private IAttdSyncRecordDAO attdSyncRecordDAO;
}
